# -*- coding: utf-8 -*-
'''
@文件    :{{ file_name }}.py
@说明    :
@时间    :2025/08/28 15:30:00
@作者    :{{ author }}
@版本    :1.0
'''
from typing import Optional, Tuple, List, Dict
from tortoise.expressions import Q
from fastapi.exceptions import HTTPException
from app.cruds.{{module_name}}.{{file_name}} import crud, Total
from app.schemas.{{module_name}}.{{file_name}} import QuerySchema, CreateSchema, UpdateSchema

class Service:

    async def list(self, payload: QuerySchema) -> Tuple[Total, List[Dict]]:
        search = Q()
        order = []
        total, objs = await crud.list(page_num=payload.pageNum, page_size=payload.pageSize, search=search, order=order)
        result = []
        for obj in objs:
            item = obj.to_dict()
            result.append(item)
        return total, result

    async def create(self, data: CreateSchema) -> Dict:
        obj =  await crud.create(data)
        return obj.to_dict()
    
    async def update(self, data: UpdateSchema) -> Optional[Dict]:
        obj = await crud.get_by_id(data.id)
        if not obj:
            raise HTTPException(status_code=200, detail="数据不存在")
        data_dict = data.model_dump(exclude={'id'})
        obj = await crud.update(obj, data_dict)
        return obj.to_dict()

    async def active(self, id: int) -> Optional[Dict]:
        obj = await crud.get_by_id(id)
        if not obj:
            raise HTTPException(status_code=200, detail="数据不存在")
        obj = await crud.update(obj, {
            "active": not obj.active
        })
        return obj.to_dict()
    
    async def delete(self, id: int):
        obj = await crud.get_by_id(id)
        if not obj:
            raise HTTPException(status_code=200, detail="数据不存在")
        await crud.delete(obj)

service = Service()